package com.freight.user.controller;

import com.freight.core.common.exception.FreightException;
import com.freight.core.common.utils.PageUtil;
import com.freight.core.common.utils.ResultUtil;
import com.freight.core.common.vo.PageVo;
import com.freight.core.common.vo.Result;
import com.freight.core.common.vo.SearchVo;
import com.freight.file.constants.FileConstant;
import com.freight.file.vo.CompanyPicFileVo;
import com.freight.user.dto.CompanyLicenseDto;
import com.freight.user.entity.CompanyLicense;
import com.freight.user.service.CompanyLicenseService;
import com.freight.user.service.CompanyUserService;
import com.freight.user.vo.CompanyEnterpriseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author huyingtai
 */
@Slf4j
@RestController
@Api(description = "营业资格证管理接口")
@RequestMapping("/freight/companyLicense")
@Transactional
public class UserCompanyLicenseController {

    @Autowired
    private CompanyLicenseService companyLicenseService;

    @Autowired
    private CompanyUserService companyUserService;

    @RequestMapping(value = "/getCompanyEnterprise", method = RequestMethod.POST)
    @ApiOperation(value = "获取企业货主信息")
    public Result<CompanyEnterpriseVo> getCompanyEnterprise(Long userId) {
        CompanyEnterpriseVo res = companyLicenseService.getCompanyEnterprise(userId);
        return ResultUtil.data(res);
    }

    @RequestMapping(value = "/updateRegistStatus", method = RequestMethod.GET)
    @ApiOperation(value = "营业执照信息审核接口")
    public Result<String> updatecompanyStatus(Long userId,Integer companyStatus,String reason) {
        String res = companyUserService.updatecompanyStatus(userId,companyStatus,reason);
        return new ResultUtil().setData(res);
    }

    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<Page<CompanyLicense>> getByCondition(CompanyLicense companyLicense, SearchVo searchVo, PageVo pageVo) {
        Page<CompanyLicense> page = companyLicenseService.findByCondition(companyLicense, searchVo, PageUtil.initPage(pageVo));
        return new ResultUtil<Page<CompanyLicense>>().setData(page);
    }

    @RequestMapping(value = "/getCompanyLicenseInfo", method = RequestMethod.POST)
    @ApiOperation(value = "获取公司营业执照")
    public Result<CompanyLicense> getCompanyLicenseInfo(Long userId) {
        CompanyLicense res = companyLicenseService.getCompanyLicenseInfo(userId);
        return ResultUtil.data(res);
    }

    @RequestMapping(value = "/upLoadCompanyLicense", method = RequestMethod.POST)
    @ApiOperation(value = "上传公司营业资格证")
    public Result<CompanyPicFileVo> upLoadCompanyLicense(@RequestParam(required = false) MultipartFile file) {
        CompanyPicFileVo res;
        try {
            res = companyLicenseService.upLoadCompanyLicense(file, FileConstant.COMPANY_WORK_PIC);
        } catch (Exception e) {
            throw new FreightException("上传失败：" + e.getMessage());
        }
        return ResultUtil.data(res);
    }


    @RequestMapping(value = "/saveCompanyLicenseInfo", method = RequestMethod.POST)
    @ApiOperation(value = "保存企业货主信息")
    public Result<String> saveCompanyLicenseInfo(CompanyLicenseDto companyLicenseDto) {
        companyLicenseService.saveCompanyLicenseInfo(companyLicenseDto);
        return ResultUtil.data("保存成功");
    }

}
